from dataloader import DataLoader
from net import logit_regrassion
import pandas as pd
from tqdm import tqdm
import matplotlib.pyplot as plt
import numpy as np

# 当数据量很小的时候我们使用留一法 每一次取一个样本作为测试样本 其它为训练样本
raw_data = pd.read_csv('./3.0a.csv')
indexs = list(range(len(raw_data) - 1))
indexs = [x + 1 for x in indexs]
x_label = ['密度', '含糖率']
y_label = ['好瓜']
net = logit_regrassion()
epoch = 100
total_num = 0
right_num = 0
with tqdm(total=epoch * 16) as pbar:
    for i in range(epoch):
        for index in indexs:
            train_data = pd.concat([raw_data.loc[:index - 2], raw_data.loc[index:]])
            val_data = pd.DataFrame(raw_data.loc[index - 1]).T
            # 训练
            for train_x, label in DataLoader(train_data, x_label, y_label):
                net.backward(train_x, label)

            # 测试
            for val_x, label in DataLoader(val_data, x_label, y_label):
                # print(net.forward(val_x), label[0])
                if net.forward(val_x) == label:
                    right_num += 1
                total_num += 1

            pbar.update(1)
        
print(right_num / total_num)

# 得到参数并写入
w = net.get_w()
np.save('w', w)